from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
browser.get('https://www.12306.cn/index/')
id_word = 'fromStationText'

# 定位到12306的出发地
locator = (By.ID, id_word)
# 开始解析网页，强制等待10秒钟，如果没有返回网页就自动抛出异常
wait = WebDriverWait(browser, 10).until(EC.presence_of_element_located(locator))
# 获取输入框
input = browser.find_element_by_id(id_word)
# 清空输入框
input.clear()
# 向输入框输入出发地
input.send_keys('厦门北')
browser.find_element_by_id('citem_0').click()

# 定位到目的地
to_id_word = 'toStationText'
# 获取目的地输入框
input = browser.find_element_by_id(to_id_word)
input.clear()
input.send_keys('黄山')
browser.find_element_by_id('citem_0').click()

# 定位到日期输入框
date_word = 'train_date'
# 获取目的地输入框
input = browser.find_element_by_id(date_word)
input.clear()
input.send_keys('2023-05-01')
time.sleep(1)

# //*[@id="search_one"]
btn = browser.find_element_by_xpath('//*[@id="search_one"]')
btn.click()

time.sleep(5)

# data = browser.page_source
# print(data)

browser.quit()
